const moment = require('moment');
module.exports = app => {
  const {STRING, INTEGER, DATE, BIGINT, TEXT, BOOLEAN} = app.Sequelize;
  const Article = app.model.define('article', {
    id: {
      type: BIGINT,
      primaryKey: true,
      autoIncrement: true
    },
    title: TEXT,
    sub_title: TEXT,
    content: TEXT,
    type: BIGINT,
    author: STRING(50),
    create_time: {
      type: DATE,
      get() {
        return moment(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    modify_time: {
      type: DATE,
      get() {
        return moment(this.getDataValue('modify_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    view_count: INTEGER.UNSIGNED,
    modify_user_id: BIGINT,
    source: STRING(50),
    keywords: STRING(100),
    img: STRING(100),
    file_ids: BIGINT,
    publish: BOOLEAN,
    publish_time: {
      type: DATE,
      get() {
        return moment(this.getDataValue('publish_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    attachments_notation: STRING(255),
    relative_article: STRING(255),
  }, {
    tableName: 'hy_article',
    timestamps: false
  });

  Article.associate = function() {
    
    app.model.Article.belongsTo(app.model.ArticleType, {
      as: 'articleType',
      foreignKey: 'type',
      targetKey: 'id'
    });

    app.model.Article.belongsTo(app.model.User, {
      as: 'user',
      foreignKey: 'modify_user_id',
      targetKey: 'id'
    })

    app.model.Article.belongsTo(app.model.Resource, {
      as: 'file',
      foreignKey: 'file_ids',
      targetKey: 'id'
    })
  };

  return Article;
};
